$().ready(function () {
    /**
     * 给seat-item 绑定点击事件
     */
    $(".seat-item").click(function () {
        // 设置验票显示
        $(this).children(".checking-tip").show();

        // 将所有名字为seat-type的input框设置为非选择状态
        $("input[name='seat-type']").attr("checked", false)
        // 所有 .seat-item div边框设置为默认样式
        $(".seat-item").css("border", "2px solid gainsboro")
        // 设置当前选项div的边框颜色
        $(this).css("border", "2px solid #39A527")
        $(this).find("input").prop("checked", "checked");

        // 设置座位变化
        $(".J_SeatType").text($(this).find("label").text())

        query_seat($(this));
    })

    /**
     * 给 form 表单中的submit 按钮绑定点击事件
     * @param obj
     */
    // $("input[type='submit']").click(function () {
    $("#submit").click(function () {
        // 列车相关的基本信息
        let dep_date = $("#dep_date").text().trim();
        let vehicle_no = $("input[name='vehicleNo']").val().trim();
        let vehicle_id = $("input[name='vehicleId']").val().trim();
        let dep_station = $("input[name='depStation']").val().trim();
        let arr_station = $("input[name='arrStation']").val().trim();
        let arr_time = $("input[name='arrTime']").val().trim();
        let dep_time = $("input[name='depTime']").val().trim();
        let seat_type = $("input[name='seatType']:checked").val();

        // 获取乘客信息
        let passenger_list = [];  // 存储乘客信息的临时数组

        // 联系人信息
        // 获取乘客数量
        let passenger_amount = $(".passenger-info").find("table").length;
        for (let i = 0; i < passenger_amount; i++) {
            let passenger_name = $("#passenger_name_" + (i + 1)).val();
            let passenger_card_type = $("#passenger_card_type_" + (i + 1)).val();
            let passenger_card_no = $("#passenger_card_no_" + (i + 1)).val();
            let passenger_birth = $("#passenger_birth_" + (i + 1)).val();

            passenger_list.push({
                "name": passenger_name,
                "cardType": passenger_card_type,
                "cardNo":passenger_card_no,
                "birth":passenger_birth
            })
        }

        // 获取联系人信息
        let contacts = $("input[name='contact']").val();
        let tel = $("input[name='tel']").val();
        let email = $("input[name='email']").val();

        // 提交信息，创建订单
        let order = {}
        // 向order中加入订单相关的信息
        order["depDate"] = dep_date;
        order["vehicleNo"] = vehicle_no;
        order["vehicleId"] = vehicle_id;
        order["depStation"] = dep_station;
        order["arrStation"] = arr_station;
        order["arrTime"] = arr_time;
        order["depTime"] = dep_time;
        order["seatType"] = seat_type;
        order["passengerModelList"] = passenger_list;
        order["contacts"] = contacts;
        order["tel"] = tel;
        order["email"] = email;

        console.log(order)
        $.ajax({
            url:"/train/order",
            contentType: "application/json; charset=UTF-8",
            data: JSON.stringify(order),
            method:"post",
            success: function (data, status, xhr) {
                // 用户订单数据提交成功，则返回到订单确认页面
                if (data.msg != "success" && data.code == -1013) {
                    console.log(data.msg)
                    location.href = data.redirectURL;
                    return;
                }

                if (data.msg == "success") {
                    location.href = data.data;
                }
            }
        })
        return false;
    })

    function query_seat(obj) {
        $.ajax({
            type :"get",
            url: ["/train/seat?vehicleNo=", $(".train-num").text().trim()].join(""),
            success:function (data, status, xhr) {
                // 隐藏验票提示信息
                console.log(data)
                obj.children(".checking-tip").hide();
                data = data.data;
                if (data > 0 && data < 50) {
                    // 显示票少的提示信息
                    obj.children(".few-tip").show();
                    obj.children(".soldout-tip").hide();
                } else if (data <= 0) {
                    // 显示无票的提示信息
                    obj.children(".few-tip").hide();
                    obj.children(".soldout-tip").show();
                    obj.find("input").prop("disabled", "true")
                }
            }
        })
    }

    /**
     * 添加乘客
     */
    $("#add_passenger").click(function () {
        // 获取passenger-info div 中table数量
        let current_passenger_amount = $(".passenger-info").find("table").length;

        let html =
            "<table id='passenger_" + (current_passenger_amount + 1) +"'>\n" +
            "<tr><td><input type=\"button\" value=\"删除\" onclick=\"del_('passenger_" + (current_passenger_amount + 1) + "')\" /></td></tr>" +
            "                    <tr>\n" +
            "                        <td class=\"label_\">姓名：</td>\n" +
            "                        <td><span class=\"star\">*</span></td>\n" +
            "                        <td><input type='text' name='name' id='passenger_name_" + (current_passenger_amount + 1) + "'/></td>\n" +
            "                    </tr>\n" +
            "                    <tr>\n" +
            "                        <td class=\"label_\">证件类型：</td>\n" +
            "                        <td><span class=\"star\">*</span></td>\n" +
            "                        <td>\n" +
            "                            <select name='cardType' id='passenger_card_type_" + (current_passenger_amount + 1) + "'>\n" +
            "                                <!-- <option value=\"identity\">身份证</option>\n" +
            "                                <option value=\"passport\">护照</option>\n" +
            "                                <option value=\"huixiang\">港澳通行证</option>\n" +
            "                                <option value=\"taibao\">台湾通行证</option> -->\n" +
            "                                <option value=\"1\">身份证</option>\n" +
            "                                <option value=\"2\">护照</option>\n" +
            "                                <option value=\"3\">港澳通行证</option>\n" +
            "                                <option value=\"4\">台湾通行证</option>\n" +
            "                            </select>\n" +
            "                        </td>\n" +
            "                    </tr>\n" +
            "                    <tr>\n" +
            "                        <td class=\"label_\">证件号码：</td>\n" +
            "                        <td><span class=\"star\">*</span></td>\n" +
            "                        <td><input type='text' name='cardNo' id='passenger_card_no_" +(current_passenger_amount + 1) +"'/></td>\n" +
            "                    </tr>\n" +
            "                    <tr>\n" +
            "                        <td class=\"label_\">生日：</td>\n" +
            "                        <td><span class=\"star\">*</span></td>\n" +
            "                        <td><input type='date' name='birth' id='passenger_birth_" + (current_passenger_amount + 1) + "'/></td>\n" +
            "                    </tr>\n" +
            "                </table>"

        // 获取 table数量
        $("#passenger_" + current_passenger_amount).parent().append(html);

        return false;
    })


})

function del_(id) {
    $("#" + id).remove();
}